package com.hailiao.devicebasicsdkdemo.utils

import android.content.Context
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar

/**
 * 显示Toast消息
 * @param msg 消息内容
 * @param duration 显示时间长度
 */
fun Context.showToast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this, null, duration).apply {
        setText(msg)
        show()
    }
}

/**
 * 显示SnackBar
 * @param msg 消息内容
 * @param duration 显示时间长度
 */
fun View.showSnackBar(msg: String, duration: Int = Snackbar.LENGTH_LONG) {
    Snackbar.make(this, msg, duration).show()
}

/**
 * 判断手机位置信息是否打开
 * @return [Boolean] true:已打开; false:未打开;
 */
fun Context.isLocationServerEnabled(): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        val locationMode =  Settings.Secure.getInt(
                contentResolver,
                Settings.Secure.LOCATION_MODE
        )
        locationMode != Settings.Secure.LOCATION_MODE_OFF
    } else {
        val locationProviders = Settings.Secure.getString(
                contentResolver,
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED
        )
        !TextUtils.isEmpty(locationProviders)
    }
}